package com.lc.wxauth.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.lc.wxauth.config.properties.WxMaProperties;
import com.lc.wxauth.config.properties.WxPayProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/9/14 14:33
 */
@Configuration
@RequiredArgsConstructor
public class WxConfiguration {

    @Resource
    private WxMaProperties wxMaProperties;

    @Resource
    private WxPayProperties wxPayProperties;


    /**
     * 构造微信授权服务对象
     */
    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxMaProperties.getAppId());
        config.setSecret(wxMaProperties.getAppSecret());
        config.setMsgDataFormat(wxMaProperties.getMsgDataFormat());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }


    /**
     * 构造微信支付服务对象
     */
    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setMchId(wxPayProperties.getMchId());
        payConfig.setAppId(wxPayProperties.getAppId());
        payConfig.setCertSerialNo(wxPayProperties.getCertSerialNo());
        payConfig.setKeyPath(wxPayProperties.getKeyPath());
        payConfig.setPrivateKeyPath(wxPayProperties.getPrivateKeyPath());
        payConfig.setPrivateCertPath(wxPayProperties.getPrivateCertPath());
        payConfig.setApiV3Key(wxPayProperties.getApiV3Key());
        payConfig.setNotifyUrl(wxPayProperties.getNotifyUrl());
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
